import 'package:redux/redux.dart';
import 'package:flutter/material.dart';
import 'package:connectivity/connectivity.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:stock_check/common/dao/UserDao.dart';
import 'package:stock_check/common/model/app.dart';
import 'package:stock_check/common/model/userInfo.dart';
import 'package:stock_check/common/redux/appReducer.dart';
import 'package:stock_check/common/redux/reduxStore.dart';
import 'package:stock_check/common/utils/NavigatorUtils.dart';

class MyDrawer extends StatefulWidget {
  final dynamic callback;

  MyDrawer(this.callback);

  @override
  _MyDrawerState createState() => _MyDrawerState();
}

class _MyDrawerState extends State<MyDrawer> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (widget.callback != null) {
      widget.callback(true);
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    if (widget.callback != null) {
      widget.callback(false);
    }
  }

  Widget getNetStatus(ConnectivityResult result) {
    const style1 =
        TextStyle(color: Colors.lightGreenAccent, fontWeight: FontWeight.bold);
    switch (result) {
      case ConnectivityResult.wifi:
        return Text(
          "wifi在线",
          style: style1,
        );
      case ConnectivityResult.mobile:
        return Text("蜂窝移动数据在线",style: style1,);
      case ConnectivityResult.none:
        return Text("离线模式");
    }
    return null;
  }

  List<Widget> getThemesWidget(Store store, App appInfo) {
    Map<String, dynamic> map = {
      "blue": Colors.blue,
      "dark": Colors.blueGrey,
      "deepOrange": Colors.deepOrange
    };
    List<Widget> list = [];
    map.forEach((k, v) {
      list.add(
        Container(
          width: 20.0,
          height: 20.0,
          margin: EdgeInsets.only(right: 20.0),
          decoration: BoxDecoration(color: v),
          child: FlatButton(
              onPressed: () {
                appInfo.themeType = k;
                store.dispatch(UpdateAppAction(appInfo));
              },
              child: null),
        ),
      );
    });

    return list;
  }

  @override
  Widget build(BuildContext context) {
    final double height = MediaQuery.of(context).size.height;;
    return Material(
      child: StoreBuilder<StockState>(
        builder: (context, store) {
          UserInfo user = store.state.userInfo;
          App app = store.state.appInfo;
          return Drawer(
            child: SafeArea(
              top: false,
              child: ListView(
                padding: EdgeInsets.only(),
                children: <Widget>[
                  UserAccountsDrawerHeader(
                    decoration: BoxDecoration(color: Theme.of(context).primaryColor),
                    currentAccountPicture: CircleAvatar(
                      backgroundImage: NetworkImage(
                          "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3245246320,3520849533&fm=26&gp=0.jpg"),
                        ),
                    accountEmail: Row(
                      children: <Widget>[
                        Text(
                          "${user.name}  ",
                          style: TextStyle(fontSize: 18.0),
                        ),
                        getNetStatus(app.netWork)
                      ],
                    ),
                    accountName: null,
                  ),
                  ListTile(
                    dense: true,
                    leading: Icon(
                      Icons.color_lens,
                      size: 30.0,
                      color: Colors.redAccent,
                    ),
                    title: Row(
                      children: getThemesWidget(store, app),
                    ),
                    trailing: Text(
                      "切换主题",
                      style: TextStyle(fontSize: 17.0),
                    ),
                  ),
                  Divider(),
                  ListTile(
                    leading: Icon(Icons.update,size: 30.0,
                      color: Colors.redAccent,),
                    title: Text('数据更新时间: ${store.state.worksInfo.lastUpdateDay==null?'暂无更新':store.state.worksInfo.lastUpdateDay}',
                    style: TextStyle(
                      color: Colors.blue,
                      fontSize: 15.0
                    ),),
                  ),
                  Divider(),
//                  ListTile(
//                    dense: true,
//                    leading:  Icon(Icons.clear_all,size: 30.0,
//                      color: Colors.redAccent,),
//                    title: FlatButton(onPressed: () async{
//                      await DataBase().deleteTable();
//                      await DataBase().deleteTaskTable();
//                      NavigatorUtils.goHome(context);
//                    }, child: Text('清除缓存',style: TextStyle(
//                      color: Colors.white
//                    ),),color: Colors.blue,),
//                  ),
//                  Divider(),
                  Container(
                    padding: EdgeInsets.only(top: height/2-120,left: 10.0,right: 10.0),
                    child: FlatButton(
                      padding: EdgeInsets.only(top: 8.0,bottom: 8.0),
                      color: Colors.blue,
                      textColor: Colors.white,
                      child: Text(
                        "切换账户",
                        style: TextStyle(
                            fontSize: 18.0, fontWeight: FontWeight.bold),
                      ),
                      onPressed: () {
                        UserDao.logout(store).then((res){
                          NavigatorUtils.goLogin(context);
                        });
                      },
                    ),
                  )
                ],
              ),
            ),
          );
        },
      ),
    );
  }
}
